package com.cjm.one.model.custom.dto;

import lombok.*;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserStatsDTO implements Serializable {
    private static final long serialVersionUID = 1L;
    // 核心指标
    private Long newUsers;                // 新增用户
    private Long activeUsers;              // 活跃用户（DAU）
    private Long paidUsers;                // 有效付费用户
    private BigDecimal retentionRate;         // 7日留存率

    // 增长率指标
    private BigDecimal newUserGrowthRate;     // 新增用户环比
    private BigDecimal activeUserGrowthRate;  // 活跃用户环比
    private BigDecimal paidUserGrowthRate;    // 付费用户环比
    private BigDecimal retentionRateGrowth;   // 留存率环比

    // 增长率计算方法
    public void calculateGrowthRates(UserStatsDTO previous) {
        this.newUserGrowthRate = calculateRate(this.newUsers, previous.newUsers);
        this.activeUserGrowthRate = calculateRate(this.activeUsers, previous.activeUsers);
        this.paidUserGrowthRate = calculateRate(this.paidUsers, previous.paidUsers);
//        this.retentionRateGrowth = calculateRate(this.retentionRate ,previous.retentionRate);
    }

    private BigDecimal calculateRate(Long current, Long before) {
        if (before == null || before == 0) return BigDecimal.ZERO;
        return new BigDecimal((current - before) * 100.0 / before)
                .setScale(2, RoundingMode.HALF_UP);
    }
    public UserStatsDTO setActiveUsers(Long activeUsers) {
        this.activeUsers = activeUsers;
        return this;
    }

    public UserStatsDTO setNewUsers(Long newUsers) {
        this.newUsers = newUsers;
        return this;
    }
    public UserStatsDTO setPaidUsers(Long paidUsers) {
        this.paidUsers = paidUsers;
        return this;
    }
    public UserStatsDTO setRetentionRate(BigDecimal retentionRate) {
        this.retentionRate = retentionRate;
        return this;
    }

}